<template>
  <div class="app-container" v-loading="loading">

    <el-form
        ref="queryRef"
        :inline="true"
        :model="queryParams"
        class="form-box form-input"
    >
      <div style="display: flex;flex-wrap: wrap; width: 100%">
        <el-col :span="4">
          <el-form-item>
            <el-input
                v-model="queryParams.expressCode"
                clearable
                placeholder="快递编号"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-input
                v-model="queryParams.expressName"
                clearable
                placeholder="快递公司"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-button icon="Search" type="primary" @click="getList">查询</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-col>
      </div>
    </el-form>
    <el-row>
      <el-button
          icon="Plus"
          type="primary"
          @click="handleAdd"
          v-hasPermi="['basic:express:save']"
      >新建快递
      </el-button>
      <el-button type="danger" @click="batchDelete"  v-hasPermi="['basic:express:delete']">删除所选</el-button>
    </el-row>
    <vxe-table
        ref="tableRef"
        style="margin-top:15px"
        :data="tableData"
        :height="tableHeight"
        align="center"
        :row-config="{isCurrent: true, isHover: true}"
        :checkbox-config="{ trigger:'row'}"
        border
    >
      <vxe-column type="checkbox" width="80"></vxe-column>
      <vxe-column type="seq" title="序号" width="70px"/>
      <vxe-column field="companyName"  width="150" title="公司名称" ></vxe-column>
      <vxe-column field="warehouseName" width="150" title="仓库"/>
      <vxe-column field="expressCode" title="快递编号"/>
      <vxe-column field="expressName" title="快递公司"/>
      <vxe-column field="url" title="网址"/>
      <vxe-column field="linkman" title="联系人"/>
      <vxe-column field="phone" title="联系电话"/>
      <vxe-column field="address" title="地址"/>
      <vxe-column field="remark" title="备注"/>
      <vxe-column fixed="right" title="操作" width="140px">
        <template #default="{row}">
          <el-button type="text" @click="clickEdit(row)"  v-hasPermi="['basic:express:save']">编辑</el-button>
          <el-button type="text" style="color:green" @click="clickView(row)">查看</el-button>
          <el-button type="text" style="color:red" @click="handleDelete(row)"  v-hasPermi="['basic:express:delete']">删除</el-button>
        </template>
      </vxe-column>
    </vxe-table>
    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
    <EditExpress :isShowDialog="isShowDialog" v-if="isShowDialog" :title="title" :rowData="rowData"
                 :isView="isView"
                 @cancel="handleClose"></EditExpress>
  </div>
</template>

<script setup>
import {getCurrentInstance, onMounted, ref, nextTick} from "vue";
import EditExpress from "@/views/basicData/expressManagement/editExpress.vue";
import {expressDelete, getExpressList} from "@/api/scApi";

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  code: "",
  name: "",
})
const tableData = ref([{}])
let total = ref(1)
const tableHeight = ref(0);
let isShowDialog = ref(false)
let isView = ref(false)
const title = ref('')
const {proxy} = getCurrentInstance();
let loading = ref(false)

function handleAdd() {
  isShowDialog.value = true;
  title.value = "新增快递";
}

onMounted(() => {
  nextTick(() => {
    tableHeight.value = window.innerHeight - 370;
  })
  window.onresize = () => {
    tableHeight.value = document.body.scrollHeight - 370;
  }
  getList()
})

function getList() {
  loading.value = true
  getExpressList({
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize,
    param: queryParams.value
  }).then(res => {
    loading.value = false
    tableData.value = res.data
    total.value = res.total
  }).finally(() => {
    loading.value = false
  })
}

const resetQuery = () => {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    code: "",
    name: "",
  }
  getList();
};

// 编辑按钮
let rowData = ref({})

async function clickEdit(row) {
  rowData.value = row
  title.value = "编辑快递";
  isShowDialog.value = true
}

// 删除按钮
function handleDelete(row) {
  deleteData([row.expressId])
}

//删除所选
function batchDelete() {
  const $table = proxy.$refs.tableRef
  if ($table) {
    const selectRecords = $table.getCheckboxRecords()
    let ids = selectRecords.map(item => item.expressId)
    deleteData(ids)
  }
}

function deleteData(ids) {
  if (!ids || ids.length === 0) {
    return
  }
  proxy.$modal.confirm('是否确认删除该数据？').then(() => {
    expressDelete({ids: ids}).then(res => {
      if (res.code === 200) {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      }
    })
  }).catch(() => {
  });
}

function clickView(row) {
  rowData.value = row
  title.value = "查看快递"
  isShowDialog.value = true
  isView.value = true
}

function handleClose(isReload) {
  rowData.value = {}
  isShowDialog.value = false
  isView.value = false
  if (isReload) {
    getList()
  }
}

</script>

<style lang="scss" scoped>

</style>
